package com.jiangjun.fetch.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.jiangjun.fetch.FetchRss;
import com.jiangjun.fetch.mongo.MongoDBSupport;
import com.jiangjun.fetch.util.UUIDUtil;
import com.mongodb.BasicDBObject;

public class AddRssAction extends DispatchAction {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String url = request.getParameter("url");
		String xpath = request.getParameter("xpath");
		String des = request.getParameter("des");
		des = new String(des.getBytes("iso-8859-1"), "utf-8");
		String type = request.getParameter("type");
		String isRssXml = request.getParameter("isRssXml");

		MongoDBSupport mongoDBSupport = MongoDBSupport.getInstance();
		BasicDBObject param = new BasicDBObject();
		param.put("url", url);
		if (null != mongoDBSupport.queryOneByParam(MongoDBSupport._FETCHRSS,
				param, null)) {
			return mapping.findForward("success");
		}
		FetchRss fetchRss = new FetchRss();
		fetchRss.setId(UUIDUtil.getUUID());
		fetchRss.setUrl(url);
		fetchRss.setDes(des);
		fetchRss.setXpath(xpath);
		fetchRss.setType(type);
		fetchRss.setIsRssXml(isRssXml);
		fetchRss.setDate("");
		fetchRss.setIsDelete("1");

		mongoDBSupport.save(fetchRss.toDbObject(), "fetchRss");
		return mapping.findForward("success");
	}

}
